class TaskPro {
  taskList = [];
  currentIndex = 0;
  isRunning = false;
  addTask(task) {
    this.taskList.push(task);
  }
  run() {
    if (this.isRunning) {
      return;
    }
    this.isRunning = true;
    this.runTask();
  }
  async runTask() {
    if (this.currentIndex >= this.taskList.length) {
      this.currentIndex = 0;
      this.isRunning = false;
      return;
    }
    const taskIndex = this.currentIndex;
    const currentTask = this.taskList[taskIndex];
    await currentTask(this.next);
    if (taskIndex === this.currentIndex) {
      //没有在任务中执行下一个，手动执行
      await this.next();
    }
  }
  next = async () => {
    this.currentIndex++;
    await this.runTask();
  };
}
const t = new TaskPro();
t.addTask(async (next) => {
  console.log('task1 start');
  await next();
  console.log('task1 end');
});
t.addTask((next) => {
  console.log('task2');
});
t.addTask((next) => {
  console.log('task3');
});
t.run();
